#!/usr/bin/env python
# -*- coding:utf-8 -*-

import json
def input_change(**dic):
    #求出添加backend的域名，组成列表,再转换成字符串，以备和server行合并；
    domain=dic.get('backend')
    global user_domain      #声明全局变量，add（）和del（）里要用
    user_domain=''.join(["backend ",domain,'\n'])
    #求出后端服务器地址，权重，最大连接数，组成列表，join成字符串；
    record=dic.get('record')
    server=str(record['server'])
    weight=str(record['weight'])
    maxconn=str(record['maxconn'])
    global user_server
    user_server=' '.join(["        server",server,"weight",weight,"maxconn",maxconn,'\n'])

def getdata():
    with open('config','r') as f:
        li_config=f.readlines()
    config_dic={}
    title_list=[]
    for line in li_config:
        if line[0] != ' ':
            config_dic[line]=[]
            title_num=li_config.index(line)
            title_list.append(line)
        elif line[0] == ' ' and line != '':
            config_dic[li_config[title_num]].append(line)
        else:
            print 'format error'
    return title_list,config_dic

##########################################################
#add和del里都要用到
def write_file():
    #把最后得到lines列表合并成字符串并写入源文件
    all_write=''.join(lines)
    #写入原文件
    with open('config','w') as fw:
        fw.write(all_write)
        fw.flush()

title_list= getdata()[0]
config_dic= getdata()[1]

#查看
def fetch():
    print config_dic['backend www.oldboy.org\n']

#add
def add():
    if 'backend www.oldboy.org\n'
    config_dic[]
    pass